package com.example.myapplication4;
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import android.view.View
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    // 声明所有布局中的控件
    private lateinit var imageViewLogo: ImageView
    private lateinit var textViewTitle: TextView
    private lateinit var editTextUsername: EditText
    private lateinit var editTextPassword: EditText
    private lateinit var buttonLogin: Button
    private lateinit var buttonRegister: Button
    private lateinit var progressBar: ProgressBar
    private lateinit var textViewLoading: TextView
    private lateinit var viewLoadingMask: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 加载布局（与提供的XML文件名匹配）
        setContentView(R.layout.activity_main)

        // 初始化所有控件
        initViews()

        // 设置按钮点击事件
        setButtonListeners()
    }

    // 初始化所有视图控件（ID与XML中完全一致）
    private fun initViews() {
        imageViewLogo = findViewById(R.id.imageView_logo)
        textViewTitle = findViewById(R.id.textView_title)
        editTextUsername = findViewById(R.id.editText_username)
        editTextPassword = findViewById(R.id.editText_password)
        buttonLogin = findViewById(R.id.button_login)
        buttonRegister = findViewById(R.id.button_register)
        progressBar = findViewById(R.id.progressBar)
        textViewLoading = findViewById(R.id.textView_loading)
        viewLoadingMask = findViewById(R.id.view_loading_mask)
    }

    // 设置按钮点击事件逻辑
    private fun setButtonListeners() {
        // 登录按钮逻辑
        buttonLogin.setOnClickListener {
            val username = editTextUsername.text.toString().trim()
            val password = editTextPassword.text.toString().trim()

            // 简单验证
            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            // 显示加载状态
            showLoading(true)

            // 模拟网络请求（2秒后结束）
            Handler(Looper.getMainLooper()).postDelayed({
                showLoading(false)
                Toast.makeText(this, "登录成功！欢迎回来，$username", Toast.LENGTH_SHORT).show()
            }, 2000)
        }

        // 注册按钮逻辑
        buttonRegister.setOnClickListener {
            Toast.makeText(this, "跳转到注册页面", Toast.LENGTH_SHORT).show()
            // 实际项目中可添加跳转逻辑：
            // startActivity(Intent(this, RegisterActivity::class.java))
        }
    }

    // 控制加载状态显示/隐藏
    private fun showLoading(show: Boolean) {
        val visibility = if (show) View.VISIBLE else View.GONE
        progressBar.visibility = visibility
        textViewLoading.visibility = visibility
        viewLoadingMask.visibility = visibility
        // 加载时禁用输入控件
        editTextUsername.isEnabled = !show
        editTextPassword.isEnabled = !show
        buttonLogin.isEnabled = !show
        buttonRegister.isEnabled = !show
    }
}
